package com.anyou.mall.fin.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.anyou.common.annotation.ShopScope;
import com.anyou.mall.fin.param.CertParam;
import com.anyou.mall.fin.param.IsPassParam;
import com.anyou.mall.fin.param.WithdrawalAduitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.anyou.common.annotation.Log;
import com.anyou.common.core.controller.BaseController;
import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.anyou.mall.fin.domain.StoreWithdrawalAduit;
import com.anyou.mall.fin.service.IStoreWithdrawalAduitService;
import com.anyou.common.utils.poi.ExcelUtil;
import com.anyou.common.core.page.TableDataInfo;

/**
 * 商家提现Controller
 *
 * @author gaochaohao
 * @date 2022-04-19
 */
@Api(tags = "商家提现申请")
@RestController
@RequestMapping("/fin/storeWithdrawalAduit")
public class StoreWithdrawalAduitController extends BaseController
{
    @Autowired
    private IStoreWithdrawalAduitService storeWithdrawalAduitService;

    /**
     * 查询商家提现列表
     */
    @ApiOperation("查询商家提现列表")
    @PreAuthorize("@ss.hasPermi('fin:storeWithdrawalAduit:list')")
    @GetMapping("/list")
    @ShopScope(baseTableAlias = "c1")
    public TableDataInfo<List<StoreWithdrawalAduit>> list(StoreWithdrawalAduit storeWithdrawalAduit)
    {
        startPage();
        List<StoreWithdrawalAduit> list = storeWithdrawalAduitService.selectStoreWithdrawalAduitList(storeWithdrawalAduit);
        return getDataTable(list);
    }

    /**
     * 导出商家提现列表
     */
    @ApiOperation("导出商家提现列表")
    @PreAuthorize("@ss.hasPermi('fin:storeWithdrawalAduit:export')")
    @Log(title = "商家提现", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ShopScope(baseTableAlias = "c1")
    public void export( StoreWithdrawalAduit storeWithdrawalAduit,HttpServletResponse response)
    {
        List<StoreWithdrawalAduit> list = storeWithdrawalAduitService.selectStoreWithdrawalAduitList(storeWithdrawalAduit);
        ExcelUtil<StoreWithdrawalAduit> util = new ExcelUtil<StoreWithdrawalAduit>(StoreWithdrawalAduit.class);
        util.exportExcel(response, list, "商家提现数据");
    }

    /**
     * 获取商家提现详细信息
     */
    @ApiOperation("获取商家提现详细信息")
    @PreAuthorize("@ss.hasPermi('fin:storeWithdrawalAduit:query')")
    @GetMapping(value = "/{storeWithdrawalAduitId}")
    public ApiResult<StoreWithdrawalAduit> getInfo(@PathVariable("storeWithdrawalAduitId") Long storeWithdrawalAduitId)
    {
        return ApiResult.success(storeWithdrawalAduitService.selectStoreWithdrawalAduitByStoreWithdrawalAduitId(storeWithdrawalAduitId));
    }

    /**
     * 新增商家提现
     */
    @ApiOperation("新增提现申请")
    @PreAuthorize("@ss.hasPermi('fin:storeWithdrawalAduit:add')")
    @Log(title = "商家提现", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult add(@RequestBody WithdrawalAduitParam param)
    {
        StoreWithdrawalAduit storeWithdrawalAduit = new StoreWithdrawalAduit();
        BeanUtils.copyProperties(param,storeWithdrawalAduit);
        storeWithdrawalAduit.setAuditTime(new Date());
        storeWithdrawalAduit.setBeforeBalance(param.getLastDeserved());
        storeWithdrawalAduit.setAfterBalance(param.getLastDeserved().subtract(param.getWithdrawalAmount()));
        boolean result = storeWithdrawalAduitService.insertStoreWithdrawalAduit(storeWithdrawalAduit);
        if (result) {
            return ApiResult.success();
        } else {
            return ApiResult.error("提交审核失败！");
        }
    }

    /**
     * 修改商家提现
     */
    @ApiOperation("修改商家提现")
    @PreAuthorize("@ss.hasPermi('fin:storeWithdrawalAduit:edit')")
    @Log(title = "商家提现", businessType = BusinessType.UPDATE)
    @PutMapping
    public ApiResult edit(@RequestBody StoreWithdrawalAduit storeWithdrawalAduit)
    {
        return ApiResult.success(storeWithdrawalAduitService.updateStoreWithdrawalAduit(storeWithdrawalAduit));
    }

    /**
     * 删除商家提现
     */
    @ApiOperation("删除商家提现")
    @PreAuthorize("@ss.hasPermi('fin:storeWithdrawalAduit:remove')")
    @Log(title = "商家提现", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeWithdrawalAduitIds}")
    public ApiResult remove(@PathVariable Long[] storeWithdrawalAduitIds)
    {
        return ApiResult.success(storeWithdrawalAduitService.deleteStoreWithdrawalAduitByStoreWithdrawalAduitIds(storeWithdrawalAduitIds));
    }

    /**
     *  是否通过提现申请
     */
    @ApiOperation("是否通过提现申请")
    @PreAuthorize("@ss.hasPermi('fin:storeWithdrawalAduit:isPass')")
    @PostMapping("/isPass")
    public ApiResult isPass(@RequestBody IsPassParam isPassParam) {
        return storeWithdrawalAduitService.isPass(isPassParam);
    }

    @ApiOperation("提交转账凭证")
    @PreAuthorize("@ss.hasPermi('fin:storeWithdrawalAduit:isPass')")
    @PostMapping("/editCert")
    public ApiResult editCert(@RequestBody CertParam param) {
        return storeWithdrawalAduitService.editCert(param);
    }
}
